package com.stock.demo.function.share.view;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.stock.demo.R;
import com.stock.demo.adapter.StockDetailNewsAdapter;
import com.stock.demo.framework.ui.activity.BaseNetActivity;
import com.stock.demo.framework.ui.view.MeasuredViewPager;
import com.stock.demo.function.market.presenter.MarketPresenter;
import com.stock.demo.function.market.presenter.MarketPresenterImpl;
import com.stock.demo.function.market.view.MarketView;
import com.stock.demo.function.share.presenter.StockDetailPresenter;
import com.stock.demo.function.share.presenter.StockDetailPresenterImpl;
import com.stock.demo.model.MarketDetailBean;
import com.stock.demo.model.MarketViewType;
import com.stock.demo.util.CommonUtils;
import com.stock.demo.util.SettingUtils;
import com.stock.demo.util.StockUtils;
import com.stock.demo.util.ThreadUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class StockDetailActivity extends BaseNetActivity implements StockDetailView, MarketView {

    private static final String INTENT_KEY_DETAIL = "detailBean";

    private View mView;
    private ImageView mIvFinish;
    private TextView mTvSymbol;
    private TextView mTvCurrentPrice;
    private TextView mTvChange;
    private TextView mTvChangePercent;
    private TextView mTvTopPrice;
    private TextView mTvBottomPrice;
    private TextView mTvOpenPrice;
    private TextView mTvClosePrice;
    private TextView mTvTur;
    private TextView mTvVol;
    private TextView mTvDayK;
    private TextView mTvWeekK;
    private TextView mTvMonthK;
    private TextView mTvDivideTime;
    private ImageView mIvInfoImage;
    private TextView mTvSell5Price;
    private TextView mTvSell5Count;
    private TextView mTvSell4Price;
    private TextView mTvSell4Count;
    private TextView mTvSell3Price;
    private TextView mTvSell3Count;
    private TextView mTvSell2Price;
    private TextView mTvSell2Count;
    private TextView mTvSell1Price;
    private TextView mTvSell1Count;
    private TextView mTvBuy1Price;
    private TextView mTvBuy1Count;
    private TextView mTvBuy2Price;
    private TextView mTvBuy2Count;
    private TextView mTvBuy3Price;
    private TextView mTvBuy3Count;
    private TextView mTvBuy4Price;
    private TextView mTvBuy4Count;
    private TextView mTvBuy5Price;
    private Runnable mRunRefresh;
    private TabLayout mTlIndex;
    private MeasuredViewPager mVpContent;
    private String mSymbol;
    private int mImageType = StockUtils.TYPE_DIVIDE_TIME;
    private RequestListener<String, GlideDrawable> mGlideListener;
    private RequestManager mGlideManager;
    private TextView mTvBuy5Count;
    private MarketPresenter mMarketPresenter;
    private StockDetailPresenter mStockDetailPresenter;
    private TextView mTvTrade;
    private TextView mTvAddSelfSelect;
    private MarketDetailBean mBean;
    private String[] mTitles = {"新闻", "盘口", "论股", "公告", "简况", "财务", "研报"};
    private StockDetailNewsAdapter mStockDetailNewsAdapter;

    public static void startAction(Context context, MarketDetailBean detailBean) {
        Intent intent = new Intent(context, StockDetailActivity.class);
        intent.putExtra(INTENT_KEY_DETAIL, detailBean);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        mIvFinish = (ImageView) findViewById(R.id.iv_stock_detail_finish);
        mTvSymbol = (TextView) findViewById(R.id.tv_stock_detail_symbol);
        mTvCurrentPrice = (TextView) findViewById(R.id.tv_stock_detail_current_price);
        mTvChange = (TextView) findViewById(R.id.tv_stock_detail_change);
        mTvChangePercent = (TextView) findViewById(R.id.tv_stock_detail_change_percent);
        mTvTopPrice = (TextView) findViewById(R.id.tv_stock_detail_top_price);
        mTvBottomPrice = (TextView) findViewById(R.id.tv_stock_detail_bottom_price);
        mTvOpenPrice = (TextView) findViewById(R.id.tv_stock_detail_open_price);
        mTvClosePrice = (TextView) findViewById(R.id.tv_stock_detail_close_price);
        mTvTur = (TextView) findViewById(R.id.tv_stock_detail_tur);
        mTvVol = (TextView) findViewById(R.id.tv_stock_detail_vol);
        mIvInfoImage = (ImageView) findViewById(R.id.iv_stock_detail_info_image);
        mTvSell5Price = (TextView) findViewById(R.id.tv_stock_detail_sell_5_price);
        mTvSell5Count = (TextView) findViewById(R.id.tv_stock_detail_sell_5_count);
        mTvSell4Price = (TextView) findViewById(R.id.tv_stock_detail_sell_4_price);
        mTvSell4Count = (TextView) findViewById(R.id.tv_stock_detail_sell_4_count);
        mTvSell3Price = (TextView) findViewById(R.id.tv_stock_detail_sell_3_price);
        mTvSell3Count = (TextView) findViewById(R.id.tv_stock_detail_sell_3_count);
        mTvSell2Price = (TextView) findViewById(R.id.tv_stock_detail_sell_2_price);
        mTvSell2Count = (TextView) findViewById(R.id.tv_stock_detail_sell_2_count);
        mTvSell1Price = (TextView) findViewById(R.id.tv_stock_detail_sell_1_price);
        mTvSell1Count = (TextView) findViewById(R.id.tv_stock_detail_sell_1_count);
        mTvBuy1Price = (TextView) findViewById(R.id.tv_stock_detail_buy_1_price);
        mTvBuy1Count = (TextView) findViewById(R.id.tv_stock_detail_buy_1_count);
        mTvBuy2Price = (TextView) findViewById(R.id.tv_stock_detail_buy_2_price);
        mTvBuy2Count = (TextView) findViewById(R.id.tv_stock_detail_buy_2_count);
        mTvBuy3Price = (TextView) findViewById(R.id.tv_stock_detail_buy_3_price);
        mTvBuy3Count = (TextView) findViewById(R.id.tv_stock_detail_buy_3_count);
        mTvBuy4Price = (TextView) findViewById(R.id.tv_stock_detail_buy_4_price);
        mTvBuy4Count = (TextView) findViewById(R.id.tv_stock_detail_buy_4_count);
        mTvBuy5Price = (TextView) findViewById(R.id.tv_stock_detail_buy_5_price);
        mTvBuy5Count = (TextView) findViewById(R.id.tv_stock_detail_buy_5_count);
        mTvDivideTime = (TextView) findViewById(R.id.tv_stock_detail_divide_time);
        mTvDayK = (TextView) findViewById(R.id.tv_stock_detail_day_k);
        mTvWeekK = (TextView) findViewById(R.id.tv_stock_detail_week_k);
        mTvMonthK = (TextView) findViewById(R.id.tv_stock_detail_month_k);
        mTlIndex = (TabLayout) findViewById(R.id.tl_stock_detail);
        mVpContent = (MeasuredViewPager) findViewById(R.id.vp_stock_detail);
        mTvTrade = (TextView) findViewById(R.id.tv_stock_detail_trade);
        mTvAddSelfSelect = (TextView) findViewById(R.id.tv_stock_detail_add_self_select);
    }

    @Override
    public void initData() {
        Serializable extra = getIntent().getSerializableExtra(INTENT_KEY_DETAIL);
        if (extra instanceof MarketDetailBean) {
            mMarketPresenter = new MarketPresenterImpl(this);
            mStockDetailPresenter = new StockDetailPresenterImpl(this);
            mBean = (MarketDetailBean) extra;
            initStockInfo(mBean);
            mSymbol = mBean.mSymbol;
            mGlideManager = Glide.with(this);
            mGlideListener = new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    setNetError();
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    onNetNext(resource).setNetComplete();
                    mRunRefresh = new Runnable() {
                        @Override
                        public void run() {
                            ThreadUtils.runOnSubThread(new Runnable() {
                                @Override
                                public void run() {
                                    mMarketPresenter.getStockDetail(StockUtils.getStockInfoUrl(mSymbol));
                                    System.out.println("stockDetailRefresh");
                                }
                            });
                        }
                    };
                    mTvSymbol.postDelayed(mRunRefresh, SettingUtils.getRefreshSpeed());
                    return false;
                }
            };
            mIvFinish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    StockDetailActivity.this.finish();
                }
            });
            mTvDivideTime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initImageType(0);
                }
            });
            mTvDayK.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initImageType(1);
                }
            });
            mTvWeekK.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initImageType(2);
                }
            });
            mTvMonthK.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initImageType(3);
                }
            });
            mTvTrade.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mBean != null) {
                        new TradeDialog(StockDetailActivity.this, mBean).show();
                    }
                }
            });
            mTvAddSelfSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CommonUtils.showToast("点击了添加自选");
                }
            });
            initNews();
            mGlideManager.load(StockUtils.getStockImageUrl(mSymbol, mImageType)).listener(mGlideListener).into(mIvInfoImage);
        }
    }

    private void initImageType(int position) {
        mTvDivideTime.setTextColor(getResources().getColor(R.color.colorGreyDark));
        mTvDayK.setTextColor(getResources().getColor(R.color.colorGreyDark));
        mTvWeekK.setTextColor(getResources().getColor(R.color.colorGreyDark));
        mTvMonthK.setTextColor(getResources().getColor(R.color.colorGreyDark));
        switch (position) {
            case 0:
                mTvDivideTime.setTextColor(getResources().getColor(R.color.colorPrimary));
                mImageType = StockUtils.TYPE_DIVIDE_TIME;
                break;
            case 1:
                mTvDayK.setTextColor(getResources().getColor(R.color.colorPrimary));
                mImageType = StockUtils.TYPE_DAY_K;
                break;
            case 2:
                mTvWeekK.setTextColor(getResources().getColor(R.color.colorPrimary));
                mImageType = StockUtils.TYPE_WEEK_K;
                break;
            case 3:
                mTvMonthK.setTextColor(getResources().getColor(R.color.colorPrimary));
                mImageType = StockUtils.TYPE_MONTH_K;
                break;
        }
        mTvSymbol.removeCallbacks(mRunRefresh);
        mGlideManager.load(StockUtils.getStockImageUrl(mSymbol, mImageType)).listener(mGlideListener).into(mIvInfoImage);
    }

    private void initNews() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new StockNewsFragment());
        fragments.add(new StockNewsFragment());
        fragments.add(new StockNewsFragment());
        fragments.add(new StockNewsFragment());
        fragments.add(new StockNewsFragment());
        fragments.add(new StockNewsFragment());
        fragments.add(new StockNewsFragment());
        mStockDetailNewsAdapter = new StockDetailNewsAdapter(getSupportFragmentManager(), fragments, mTitles);
        mVpContent.setAdapter(mStockDetailNewsAdapter);
        mTlIndex.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[0]));
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[1]));
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[2]));
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[3]));
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[4]));
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[5]));
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[6]));
        mTlIndex.setupWithViewPager(mVpContent);
        mTlIndex.setTabsFromPagerAdapter(mStockDetailNewsAdapter);
    }

    private void initStockInfo(MarketDetailBean bean) {
        mTvSymbol.setText(bean.mName + "-" + bean.mSymbol);
        mTvCurrentPrice.setText(bean.mCurrentPrice);
        mTvChange.setText(bean.mChange);
        mTvChangePercent.setText(bean.mChangePercent);
        mTvTopPrice.setText(bean.mTopPrice);
        mTvBottomPrice.setText(bean.mBottomPrice);
        mTvOpenPrice.setText(bean.mOpenPrice);
        mTvClosePrice.setText(bean.mClosePrice);
        mTvTur.setText(bean.mTurnover);
        mTvVol.setText(bean.mVolume);
        mTvSell5Price.setText(bean.mSellPrice5);
        mTvSell5Count.setText(bean.mSellCount5);
        mTvSell4Price.setText(bean.mSellPrice4);
        mTvSell4Count.setText(bean.mSellCount4);
        mTvSell3Price.setText(bean.mSellPrice3);
        mTvSell3Count.setText(bean.mSellCount3);
        mTvSell2Price.setText(bean.mSellPrice2);
        mTvSell2Count.setText(bean.mSellCount2);
        mTvSell1Price.setText(bean.mSellPrice1);
        mTvSell1Count.setText(bean.mSellCount1);
        mTvBuy1Price.setText(bean.mBuyPrice1);
        mTvBuy1Count.setText(bean.mBuyCount1);
        mTvBuy2Price.setText(bean.mBuyPrice2);
        mTvBuy2Count.setText(bean.mBuyCount2);
        mTvBuy3Price.setText(bean.mBuyPrice3);
        mTvBuy3Count.setText(bean.mBuyCount3);
        mTvBuy4Price.setText(bean.mBuyPrice4);
        mTvBuy4Count.setText(bean.mBuyCount4);
        mTvBuy5Price.setText(bean.mBuyPrice5);
        mTvBuy5Count.setText(bean.mBuyCount5);
    }

    @Override
    public View onCreateSuccessView() {
        mView = LayoutInflater.from(this).inflate(R.layout.activity_stock_detail, null);
        return mView;
    }

    @Override
    public void onReload(View v) {
        initData();
    }

    @Override
    public void onGetStockDetail(MarketDetailBean detailBean) {
        initStockInfo(detailBean);
        mGlideManager.load(StockUtils.getStockImageUrl(mSymbol, mImageType)).listener(mGlideListener).into(mIvInfoImage);
    }

    //空实现，无需操作
    @Override
    public void onGetMarketInfo(ArrayList<MarketViewType> marketViewTypes) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTvSymbol.removeCallbacks(mRunRefresh);
    }
}
